package cn.edu.sgu.www.mhxysy.controller.jingmai;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.jingmai.JingmaiCategoryDTO;
import cn.edu.sgu.www.mhxysy.entity.jingmai.JingmaiCategory;
import cn.edu.sgu.www.mhxysy.pager.jingmai.JingmaiCategoryPager;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.jingmai.JingmaiCategoryService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "奇经八脉管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/jingmai_category", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class JingmaiCategoryController {
    
    private final JingmaiCategoryService jingmaiCategoryService;

    @Autowired
    public JingmaiCategoryController(JingmaiCategoryService jingmaiCategoryService) {
        this.jingmaiCategoryService = jingmaiCategoryService;
    }

    @ApiOperation("上传图片")
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public JsonResult<String> upload(MultipartFile file) throws IOException {
        String url = jingmaiCategoryService.upload(file);

        return JsonResult.success(CustomMessages.UPLOAD_SUCCESS, url);
    }

    @ApiOperation("添加奇经八脉")
    @ApiOperationSupport(ignoreParameters = "id")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JsonResult<Void> insert(@Validated JingmaiCategoryDTO jingmaiCategoryDTO) {
        jingmaiCategoryService.insert(jingmaiCategoryDTO);

        return JsonResult.success(CustomMessages.INSERT_SUCCESS);
    }

    @ApiOperation("通过ID删除奇经八脉")
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public JsonResult<Void> deleteById(
            @ApiParam(name = "id", value = "奇经八脉ID", required = true)
            @NotNull(message = CustomMessages.ID_NOT_NULL) Integer id) {
        jingmaiCategoryService.deleteById(id);

        return JsonResult.success(CustomMessages.DELETE_SUCCESS);
    }

    @ApiOperation("通过ID修改奇经八脉")
    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public JsonResult<Void> updateById(@Validated JingmaiCategoryDTO jingmaiCategoryDTO) {
        jingmaiCategoryService.updateById(jingmaiCategoryDTO);

        return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
    }

    @ApiOperation("通过ID修改奇经八脉图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "奇经八脉ID", required = true),
            @ApiImplicitParam(name = "image", value = "图片地址", required = true)
    })
    @RequestMapping(value = "/updateImageById", method = RequestMethod.POST)
    public JsonResult<Void> updateImageById(
            @NotNull(message = CustomMessages.ID_NOT_NULL) Integer id,
            @NotBlank(message = CustomMessages.IMAGE_NOT_NULL) String image) {
        jingmaiCategoryService.updateImageById(id, image);

        return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
    }

    @ApiOperation("通过ID查询奇经八脉信息")
    @RequestMapping(value = "/selectById", method = RequestMethod.GET)
    public JsonResult<JingmaiCategory> selectById(
            @ApiParam(name = "id", value = "奇经八脉ID", required = true)
            @NotNull(message = CustomMessages.ID_NOT_NULL) Integer id) {
        JingmaiCategory jingmaiCategory = jingmaiCategoryService.selectById(id);

        return JsonResult.success(null, jingmaiCategory);
    }

    @ApiOperation("查询经脉大招和乾坤一脉")
    @RequestMapping(value = "/selectMaster", method = RequestMethod.GET)
    public JsonResult<List<JingmaiCategory>> selectMaster(
            @ApiParam(name = "schoolId", value = "门派ID", required = true)
            @NotNull(message = "门派" + CustomMessages.ID_NOT_NULL) Integer schoolId) {
        List<JingmaiCategory> list = jingmaiCategoryService.selectMaster(schoolId);

        return JsonResult.success(null, list);
    }

    @ApiOperation("分页查询奇经八脉列表")
    @RequestMapping(value = "/selectByPage", method = RequestMethod.POST)
    public JsonResult<JsonPage<JingmaiCategory>> selectByPage(JingmaiCategoryPager pager) {
        Page<JingmaiCategory> page = jingmaiCategoryService.selectByPage(pager);

        return JsonResult.restPage(page);
    }

    @ApiOperation("通过门派ID查询奇经八脉列表")
    @RequestMapping(value = "/selectBySchoolId", method = RequestMethod.GET)
    public JsonResult<List<JingmaiCategory>> selectBySchoolId(
            @ApiParam(name = "schoolId", value = "门派ID", required = false)
            @RequestParam(required = false) Integer schoolId) {
        List<JingmaiCategory> list = jingmaiCategoryService.selectBySchoolId(schoolId);

        return JsonResult.success(null, list);
    }

}